<%@ page language="java" contentType="text/html; charset=UTF-8"	 pageEncoding="UTF-8"%>
<%@ include file="../include/laypublic.jsp"%>
<!DOCTYPE html>
<html>
<head>
  <title>设备合同新增</title>
</head>
<style>
	.fieldset{
		margin-top: 5px;
		margin-bottom: 0px
	}
	.layui-legend{
		background-color:#5fb878;
		font-size:15px !important;
		width:140px;
	}
	.layui-form-item {
	    margin-bottom: 5px;
	    clear: both;
	    *zoom: 1;
	}
	.layui-form-label{width: 160px;}
</style>
<body>
<div class="layui-fluid">
    <div class="layui-card">
	    <div class="layui-tab-item layui-show">
	    	<div style="padding-top:15px;">&nbsp;</div>
			<form class="layui-form" id="addInfo_form" lay-filter="add_info_from">
				<fieldset class="layui-elem-field layui-field-title fieldset"  id="baseInfo">
				  <legend class="layui-btn layui-legend" style="text-align:left;cursor:default;">项目基础信息  ></legend>
				  <div style="margin-top:20px">
				  	<div class="layui-form-item">
				  		<div class="layui-inline">
					      	<label class="layui-form-label">申请公司</label>
					      	<div class="layui-input-inline">
								<input type="text" value="${bindOrgName}" style="border:0px;width:220px" readonly="readonly" class="layui-input">
					      	</div>
					    </div>
					    <div class="layui-inline">
					    	<label class="layui-form-label">申请部门</label>
				      		<div class="layui-input-inline">
								<input type="text" value="${bindDeptName}" style="border:0px;" readonly="readonly" class="layui-input">
				      		</div>
					    </div>
					    <div class="layui-inline">
					    	<label class="layui-form-label">申请人</label>
				      		<div class="layui-input-inline">
				        		<input type="text" name="staffName" value="${staffName }" readonly="readonly" style="border:0px" class="layui-input">
				      		</div>
					    </div>
				    </div>
				    <div class="layui-form-item">
						<div class="layui-inline">
							<label class="layui-form-label">项目名称</label>
							<div class="layui-input-inline">
								<select name="refId" id="add_select_proId" lay-verify="required"
									lay-filter="add_select_proId" lay-search>
									<option value="">请选择</option>
									<c:if test="${not empty innerAgrts}">
										<c:forEach items="${innerAgrts}" var="innerAgrt">
											<option value="${innerAgrt.id}">${innerAgrt.proName}_${innerAgrt.innerAgrtName}</option>
										</c:forEach>
									</c:if>
								</select>
						        <input type="hidden" id="proName" name="proName">
						        <input type="hidden" id="types" name="types" value="1">
						        <input type="hidden" id="sgCityId" name="sgCityId">
						        <input type="hidden" name="extType" value="${extType }">
							</div>
						</div>
						<div class="layui-inline">
							<label class="layui-form-label">项目编号</label>
							<div class="layui-input-inline">
								<input type="text" value="" id="proCode" name="proCode" style="border: 0px;" readonly="readonly" class="layui-input">
							</div>
						</div>
						<div class="layui-inline">
							<label class="layui-form-label">区域公司</label>
							<div class="layui-input-inline">
								<input type="hidden" value="" id="query-input-hrOrgAreaId" name="hrOrgAreaId" readonly="readonly" class="layui-input">
								<input type="text" value="" id="query-input-hrOrgAreName" style="border: 0px;width:220px" readonly="readonly" class="layui-input">
							</div>
						</div>
					</div>
				    <div class="layui-form-item">
						<div class="layui-inline">
							<label class="layui-form-label">项目公司</label>
							<div class="layui-input-inline">
								<input type="hidden" id="query-input-hrOrgProId" name="hrOrgProId" readonly="readonly" class="layui-input">
								<input type="text" value="" id="query-input-hrOrgProName" style="border: 0px;width:220px" readonly="readonly" class="layui-input">
							</div>
						</div>
			  			<div class="layui-inline">
							<label class="layui-form-label">实施机构</label>
							<div class="layui-input-inline">
								<input type="text" id="query-input-carryOrg" style="border: 0px;width:220px" name="carryOrg" readonly="readonly" class="layui-input">
							</div>
						</div>
				   		<div class="layui-inline">
							<label class="layui-form-label">项目地址</label>
							<div class="layui-input-inline">
								<input type="text" value="" id="query-input-proCityName" style="border: 0px;" readonly="readonly" class="layui-input">
							</div>
						</div>
				    </div>
				    <div class="layui-form-item">
				    	<div class="layui-inline">
							<label class="layui-form-label">项目负责人</label>
							<div class="layui-input-inline">
								<input type="text" value="" id="query-input-proLeader" style="border: 0px;" name="proLeader" readonly="readonly" class="layui-input">
							</div>
						</div>
				    	<div class="layui-inline">
							<label class="layui-form-label">设备合同名称</label>
							<div class="layui-input-inline">
								<input type="text" name="deviceCotName" maxlength="50" lay-verify="required" autocomplete="off" class="layui-input">
							</div>
						</div>
				    	<div class="layui-inline">
							<label class="layui-form-label">合同单位名称</label>
							<div class="layui-input-inline">
								<select name="sgCoopId" lay-verify="required" lay-search>
									<option value="">请选择</option>
									<c:if test="${not empty coopInfos}">
										<c:forEach items="${coopInfos}" var="coopInfo">
											<option value="${coopInfo.id}">${coopInfo.coopName}</option>
										</c:forEach>
									</c:if>
								</select>
							</div>
						</div>
				    </div>
				    <div class="layui-form-item">
					    <div class="layui-inline">
							<label class="layui-form-label">设备合同编码（选填）</label>
							<div class="layui-input-inline">
								<input type="text" name="deviceCotCode" maxlength="50" autocomplete="off" class="layui-input">
							</div>
						</div>
					    <div class="layui-inline">
							<label class="layui-form-label">合同签订日期（选填）</label>
							<div class="layui-input-inline">
								<input type="text" class="layui-input" id="sent-laydate-year" readonly name="signDate" >
							</div>
						</div>
						<div class="layui-inline">
							<label class="layui-form-label">合同总价（元）</label>
							<div class="layui-input-inline">
								<input type="text" id="sumTotal" name="sumTotal" style="border: 0px;" readonly="readonly" class="layui-input">
							</div>
						</div>
					</div>
		  		</div>
			</fieldset>
				<fieldset class="layui-elem-field layui-field-title fieldset" id="houseInfo">
			  		<legend class="layui-btn layui-legend" style="text-align:left;cursor:default;">合同价款信息  ></legend>
				  	<div style="margin-top:20px">
				  		<div class="layui-form-item" style="margin: 0 20px;">
				  			<table class="layui-table layuiadmin-page-table" lay-skin="line">
			                  <thead>
			                    <tr>
			                      <th>序号</th>
			                      <th>分部分项清单名称</th>
			                      <th>计量单位</th>
			                      <th>合同数量</th>
			                      <th>合同单价（元）</th>
			                      <th>分部分项金额（元）</th>
			                      <th></th>
			                    </tr> 
			                  </thead>
			                  <tbody>
			                    <tr id="linkRow1">
			                    	<td><input type="text" name="deviceCotPrices[0].showIdx" value="1" style="border: 0px;width:50px" readonly="readonly" class="layui-input"></td>
			                    	<td><input type="text" name="deviceCotPrices[0].subName" maxlength="50" onblur="checkName(this);" lay-verify="required" autocomplete="off" class="layui-input"></td>
			                    	<td><input type="text" name="deviceCotPrices[0].subUnit" maxlength="50" lay-verify="required" autocomplete="off" class="layui-input"></td>
			                    	<td><input type="text" name="deviceCotPrices[0].cotNums" onblur="calcSumAmount(this);" maxlength="10" lay-verify="required|floatNumber" autocomplete="off" class="layui-input"></td>
			                    	<td><input type="text" name="deviceCotPrices[0].cotUnitPrice" onblur="calcSumAmount(this);arabiaToCN(this,1);" maxlength="13" lay-verify="required|floatAmount" autocomplete="off" class="layui-input"></td>
			                    	<td><input type="text" name="deviceCotPrices[0].cotSumPrice" style="border: 0px;" readonly="readonly" class="layui-input"></td>
			                    	<td style="width:100px;">
				                    	<button type="button" class="layui-btn layui-btn-sm" id="addLinkBtn1" onclick="addRow(this);"><i class="layui-icon">&#xe654;</i></button>
							    		<button type="button" class="layui-btn layui-btn-sm" style="display:none" id="delLinkBtn1" onclick="deleteRow(1)"><i class="layui-icon">&#xe640;</i></button>
							    	</td>
			                    </tr>
			                  </tbody>
			                </table>
					    </div>
				  	</div>
				</fieldset>
				<fieldset class="layui-elem-field layui-field-title fieldset">
				  	<legend class="layui-btn layui-legend" style="text-align:left;cursor:default;">合同信息  ></legend>
				  	<div style="margin-top:20px">
					  	<div class="layui-form-item">
					  		<div class="layui-inline">
						      	<label class="layui-form-label">合同工期（年）</label>
						      	<div class="layui-input-inline">
									<input type="text" name="periodYears" maxlength="5" onblur="calcDays(this);" lay-verify="required|floatNumber" autocomplete="off" class="layui-input">
						      	</div>
						    </div>
					  		<div class="layui-inline">
						      	<label class="layui-form-label">合同工期（天）</label>
						      	<div class="layui-input-inline">
									<input type="text" id="periodDays" name="periodDays" style="border:0px;" readonly="readonly" class="layui-input">
						      	</div>
						    </div>
					  		<div class="layui-inline">
						      	<label class="layui-form-label">合同开工日期</label>
						      	<div class="layui-input-inline">
									<input type="text" id="beginBuildDate" name="beginBuildDate" lay-verify="required" readonly="readonly" class="layui-input">
						      	</div>
						    </div>
						</div>
					  	<div class="layui-form-item">
					  		<div class="layui-inline">
						      	<label class="layui-form-label">实际开工日期</label>
						      	<div class="layui-input-inline">
									<input type="text" id="actBeginDate" name="actBeginDate" lay-verify="required" readonly="readonly" class="layui-input">
						      	</div>
						    </div>
					  		<div class="layui-inline">
						      	<label class="layui-form-label">合同竣工日期</label>
						      	<div class="layui-input-inline">
									<input type="text" id="endBuildDate" name="endBuildDate" lay-verify="required" style="border:0px;" readonly="readonly" class="layui-input">
						      	</div>
						    </div>
					  		<div class="layui-inline">
						      	<label class="layui-form-label">调整后竣工日期</label>
						      	<div class="layui-input-inline">
									<input type="text" id="adjustEndDate" name="adjustEndDate" lay-verify="required" style="border:0px;" readonly="readonly" class="layui-input">
						      	</div>
						    </div>
						 </div>
						 <div class="layui-form-item">
					  		<div class="layui-inline">
						      	<label class="layui-form-label">实际竣工日期</label>
						      	<div class="layui-input-inline">
									<input type="text" id="actEndDate" name="actEndDate" lay-verify="required" readonly="readonly" class="layui-input">
						      	</div>
						    </div>
						 </div>
						 <div class="layui-form-item">
					  		<div class="layui-inline">
						      	<label class="layui-form-label">质量标准</label>
						      	<div class="layui-input-inline">
									<textarea placeholder="请输入最多500字" name="qaStandard"
											class="layui-textarea" maxlength="500" lay-verify="required"
											style="width: 899px; height: 90px;"></textarea>
						      	</div>
						    </div>
						 </div>
						 <div class="layui-form-item">
					  		<div class="layui-inline">
						      	<label class="layui-form-label">预付款比例（%）</label>
						      	<div class="layui-input-inline">
									<input type="text" name="prePayRate" value="0" lay-verify="required|floatNumber" 
										autocomplete="off" onblur="checkValueNumber(this);" class="layui-input">
						      	</div>
						    </div>
					  		<div class="layui-inline">
						      	<label class="layui-form-label">月度付款比例（%）</label>
						      	<div class="layui-input-inline">
									<input type="text" name="monthPayRate" value="70" lay-verify="required|floatNumber" 
										autocomplete="off" onblur="checkValueNumber(this);" class="layui-input">
						      	</div>
						    </div>
					  		<div class="layui-inline">
						      	<label class="layui-form-label">年度付款比例（%）</label>
						      	<div class="layui-input-inline">
									<input type="text" name="yearPayRate" value="75" lay-verify="required|floatNumber" 
										autocomplete="off" onblur="checkValueNumber(this);" class="layui-input">
						      	</div>
						    </div>
						 </div>
						 <div class="layui-form-item">
					  		<div class="layui-inline">
						      	<label class="layui-form-label">竣工验收后付款比例（%）</label>
						      	<div class="layui-input-inline">
									<input type="text" name="checkAfterRate" value="85" lay-verify="required|floatNumber" 
										autocomplete="off" onblur="checkValueNumber(this);" class="layui-input">
						      	</div>
						    </div>
					  		<div class="layui-inline">
						      	<label class="layui-form-label">结算后付款比例（%）</label>
						      	<div class="layui-input-inline">
									<input type="text" name="settleAfterRate" value="97" lay-verify="required|floatNumber" 
										autocomplete="off" onblur="checkValueNumber(this);" class="layui-input">
						      	</div>
						    </div>
					  		<div class="layui-inline">
						      	<label class="layui-form-label">质保金比例（%）</label>
						      	<div class="layui-input-inline">
									<input type="text" name="qaEnsureRate" value="3" lay-verify="required|floatNumber" 
										autocomplete="off" onblur="checkValueNumber(this);" class="layui-input">
						      	</div>
						    </div>
						 </div>
					 </div>
			 	</fieldset>
				<fieldset class="layui-elem-field layui-field-title fieldset" >
			  		<legend class="layui-btn layui-legend" style="text-align:left;cursor:default;">附件  ></legend>
					  	<div style="margin-top:20px;">
						    <div class="layui-form-item">
							    <label class="layui-form-label" style="width:350px;">支持pdf、excel、word、图片资料，可上传多个</label>
							</div>
						    <div class="layui-form-item">
							    <label class="layui-form-label">Word版合同文本（选填）</label>
							    <div class="layui-input-block">
							    	<div class="layui-upload">
						              	<button type="button" class="layui-btn" id="1-upload-more">上传</button>
				                		<div class="layui-upload-list" id="1-upload-more-list">
				                		</div>
						            </div>
							    </div>
						  	</div>
						    <div class="layui-form-item">
							    <label class="layui-form-label">PDF版合同文本（选填）</label>
							    <div class="layui-input-block">
							    	<div class="layui-upload">
						              	<button type="button" class="layui-btn" id="2-upload-more">上传</button>
				                		<div class="layui-upload-list" id="2-upload-more-list">
				                		</div>
						            </div>
							    </div>
						  	</div>
						    <div class="layui-form-item">
							    <label class="layui-form-label">盖章版合同文本（选填）</label>
							    <div class="layui-input-block">
							    	<div class="layui-upload">
						              	<button type="button" class="layui-btn" id="3-upload-more">上传</button>
				                		<div class="layui-upload-list" id="3-upload-more-list">
				                		</div>
						            </div>
							    </div>
						  	</div>
						    <div class="layui-form-item">
							    <label class="layui-form-label">其他附件（选填）</label>
							    <div class="layui-input-block">
							    	<div class="layui-upload">
						              	<button type="button" class="layui-btn" id="4-upload-more">上传</button>
				                		<div class="layui-upload-list" id="4-upload-more-list">
				                		</div>
						            </div>
							    </div>
						  	</div>
					  	</div>
				</fieldset>
				<div class="layui-form-item" style="position: fixed ! important; right: 30px; bottom:45%;z-index:10000;">
					<div class="layui-input-block">
			      		<button class="layui-btn layui-bg-blue layui-btn-normal layui-btn-lg  layui-btn-radius" type="button" lay-submit lay-filter="addInfo_form_submit" id="addInfo_form_submit">
			      		<i class="layui-icon layui-icon-ok layuiadmin-button-btn"></i>
			      		提交
			      		</button>
			      	</div>
			    </div>
			</form>
  		</div>
	</div>
</div>
  <script>
	//静态资源路径前缀
	var staticPath = '${fileHttpPre}';
	var stateJSON = <infopub:dicJson groupNo="A00010028" />;
	
  layui.config({
    base: '${ctx}/resources/layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'set', 'upload', 'element', 'laydate'], function(){
    var $ = layui.$
    ,form = layui.form
    ,element = layui.element
    ,laydate = layui.laydate
    ,upload = layui.upload;
    
    laydate.render({
	      elem: '#sent-laydate-year'
 		});
    laydate.render({
	      elem: '#beginBuildDate'
    	  ,done: function(value, date){
    		  	var periodDays = $("#periodDays").val();
    			if(periodDays == ''){
    				return;
    			}
    			if(value != ''){
    				//合同竣工日期
    				var endBuildDate = getNewData(value,periodDays);
    	  			$("#endBuildDate").val(endBuildDate);
    			}
 	  		}
 		});
    laydate.render({
	      elem: '#actBeginDate'
    	  ,done: function(value, date){
    		  	var periodDays = $("#periodDays").val();
    			if(periodDays == ''){
    				return;
    			}
    			if(value != ''){
    				//调整后竣工日期
    				var adjustEndDate = getNewData(value,periodDays);
    	  			$("#adjustEndDate").val(adjustEndDate);
    			}
 	  		}
 		});
    laydate.render({
	      elem: '#actEndDate'
 		});
 	// 正则校验
	form.verify({
		//验证汉字
	    CHS:[
	    	/^[\u0391-\uFFE5]+$/
	    	,'只能输入汉字'
	  	],
	  	//大于0的正整数
	  	intNumber:[
			/^[1-9]\d*$/
			,'请输入正确的数字'
	  	],
	  	//数字只能有两位小数
	  	floatNumber:[
			/^([-]?)([0-9](\d+)?(\.\d{1,2})?$)|(^\d\.\d{1,2}$)/
			,'请输入正确的数字'
	  	],
	  	//数字大于0，并只能有两位小数
	  	floatAmount:[
			/^([0-9](\d+)?(\.\d{1,2})?$)|(^\d\.\d{1,2}$)/
			//var pattern=/^((\d+\.\d*[1-9]\d{1})|(\d*[1-9]\d*\.\d{2}))$/; 
			,'请输入正确的数字'
	  	]
	});
	//上传附件
  	upload.render({
	      elem: '#1-upload-more'
	      ,url: basePath + '/deviceCot/uploadAtt/'
	      ,data: {
	    	  types:1
	    		}
	      ,multiple: true
	      ,accept: 'file'
	      ,acceptMime: 'application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document'
	   	  ,before: function(obj){ //obj参数包含的信息
	   		    layer.load(); //上传loading
	   		  }
	      ,done: function(res){
	    	  //如果上传失败
	          if(res.code > 0){
	        	  layer.closeAll('loading'); //关闭loading
	        	  return layer.msg(res.msg,{icon: 7});
	          }
	          //上传完毕
	   	  	  if(res.entity != null && res.entity.length > 0){
				for(var i in res.entity){
					var deviceCotAtt = res.entity[i];
					var arr = deviceCotAtt.split("|");
					var imgSrc = "";
					var suffix = arr[1].substring(arr[1].lastIndexOf(".")+1,arr[1].length);
					if(suffix == 'jpg' || suffix == 'png'){
						imgSrc = "${fileHttpPre}/"+arr[3];
					}else{
						imgSrc = "${ctx}/resources/images/"+suffix+".jpg";
					}
	        		$('#1-upload-more-list').append('<div class="layui-upload-img-div" id="upload_img_'+arr[2]+'"><img src="'+imgSrc+'" title="'+ arr[1] +'" class="layui-upload-img"><div class="mask"><a href="javascript:void(0)" onclick="showImg(\''+"${fileHttpPre}"+arr[3] +'\');">查看</a><a href="javascript:void(0)" onclick="del(\''+arr[2]+'\')";>删除</a></div><p>'+arr[1]+'</p><input type="hidden" value="'+deviceCotAtt+'" id="fileAtt_'+arr[2]+'" name="fileAtt1" ></input></div> ');
				}
		  	  }
	      }
	      ,allDone: function(obj){ //当文件全部被提交后，才触发
	    	  layer.closeAll('loading'); //关闭loading
	      }
	      ,error: function(){
	    	  layer.closeAll('loading'); //关闭loading
	          //上传失败
	          layer.msg("上传失败，请稍后再试~",{icon: 7});
	        }
	    });
	//上传附件
  	upload.render({
	      elem: '#2-upload-more'
	      ,url: basePath + '/deviceCot/uploadAtt/'
	      ,data: {
	    	  types:2
	    		}
	      ,multiple: true
	      ,accept: 'file'
	      ,acceptMime: 'application/pdf'
	   	  ,before: function(obj){ //obj参数包含的信息
	   		    layer.load(); //上传loading
	   		  }
	      ,done: function(res){
	    	  //如果上传失败
	          if(res.code > 0){
	        	  layer.closeAll('loading'); //关闭loading
	        	  return layer.msg(res.msg,{icon: 7});
	          }
	          //上传完毕
	   	  	  if(res.entity != null && res.entity.length > 0){
				for(var i in res.entity){
					var deviceCotAtt = res.entity[i];
					var arr = deviceCotAtt.split("|");
					var imgSrc = "";
					var suffix = arr[1].substring(arr[1].lastIndexOf(".")+1,arr[1].length);
					if(suffix == 'jpg' || suffix == 'png'){
						imgSrc = "${fileHttpPre}/"+arr[3];
					}else{
						imgSrc = "${ctx}/resources/images/"+suffix+".jpg";
					}
	        		$('#2-upload-more-list').append('<div class="layui-upload-img-div" id="upload_img_'+arr[2]+'"><img src="'+imgSrc+'" title="'+ arr[1] +'" class="layui-upload-img"><div class="mask"><a href="javascript:void(0)" onclick="showImg(\''+"${fileHttpPre}"+arr[3] +'\');">查看</a><a href="javascript:void(0)" onclick="del(\''+arr[2]+'\')";>删除</a></div><p>'+arr[1]+'</p><input type="hidden" value="'+deviceCotAtt+'" id="fileAtt_'+arr[2]+'" name="fileAtt2" ></input></div> ');
				}
		  	  }
	      }
	      ,allDone: function(obj){ //当文件全部被提交后，才触发
	    	  layer.closeAll('loading'); //关闭loading
	      }
	      ,error: function(){
	    	  layer.closeAll('loading'); //关闭loading
	          //上传失败
	          layer.msg("上传失败，请稍后再试~",{icon: 7});
	        }
	    });
	//上传附件
  	upload.render({
	      elem: '#3-upload-more'
	      ,url: basePath + '/deviceCot/uploadAtt/'
	      ,data: {
	    	  types:3
	    		}
	      ,multiple: true
	      ,accept: 'file'
	      ,acceptMime: 'application/zip,application/x-rar-compressed,image/jpeg,image/png,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.ms-powerpoint,application/vnd.openxmlformats-officedocument.presentationml.presentation,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/pdf'
	   	  ,before: function(obj){ //obj参数包含的信息
	   		    layer.load(); //上传loading
	   		  }
	      ,done: function(res){
	    	  //如果上传失败
	          if(res.code > 0){
	        	  layer.closeAll('loading'); //关闭loading
	        	  return layer.msg(res.msg,{icon: 7});
	          }
	          //上传完毕
	   	  	  if(res.entity != null && res.entity.length > 0){
				for(var i in res.entity){
					var deviceCotAtt = res.entity[i];
					var arr = deviceCotAtt.split("|");
					var imgSrc = "";
					var suffix = arr[1].substring(arr[1].lastIndexOf(".")+1,arr[1].length);
					if(suffix == 'jpg' || suffix == 'png'){
						imgSrc = "${fileHttpPre}/"+arr[3];
					}else{
						imgSrc = "${ctx}/resources/images/"+suffix+".jpg";
					}
	        		$('#3-upload-more-list').append('<div class="layui-upload-img-div" id="upload_img_'+arr[2]+'"><img src="'+imgSrc+'" title="'+ arr[1] +'" class="layui-upload-img"><div class="mask"><a href="javascript:void(0)" onclick="showImg(\''+"${fileHttpPre}"+arr[3] +'\');">查看</a><a href="javascript:void(0)" onclick="del(\''+arr[2]+'\')";>删除</a></div><p>'+arr[1]+'</p><input type="hidden" value="'+deviceCotAtt+'" id="fileAtt_'+arr[2]+'" name="fileAtt3" ></input></div> ');
				}
		  	  }
	      }
	      ,allDone: function(obj){ //当文件全部被提交后，才触发
	    	  layer.closeAll('loading'); //关闭loading
	      }
	      ,error: function(){
	    	  layer.closeAll('loading'); //关闭loading
	          //上传失败
	          layer.msg("上传失败，请稍后再试~",{icon: 7});
	        }
	    });
	//上传附件
  	upload.render({
	      elem: '#4-upload-more'
	      ,url: basePath + '/deviceCot/uploadAtt/'
	      ,data: {
	    	  types:4
	    		}
	      ,multiple: true
	      ,accept: 'file'
	      ,acceptMime: 'application/zip,application/x-rar-compressed,image/jpeg,image/png,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.ms-powerpoint,application/vnd.openxmlformats-officedocument.presentationml.presentation,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/pdf'
	   	  ,before: function(obj){ //obj参数包含的信息
	   		    layer.load(); //上传loading
	   		  }
	      ,done: function(res){
	    	  //如果上传失败
	          if(res.code > 0){
	        	  layer.closeAll('loading'); //关闭loading
	        	  return layer.msg(res.msg,{icon: 7});
	          }
	          //上传完毕
	   	  	  if(res.entity != null && res.entity.length > 0){
				for(var i in res.entity){
					var deviceCotAtt = res.entity[i];
					var arr = deviceCotAtt.split("|");
					var imgSrc = "";
					var suffix = arr[1].substring(arr[1].lastIndexOf(".")+1,arr[1].length);
					if(suffix == 'jpg' || suffix == 'png'){
						imgSrc = "${fileHttpPre}/"+arr[3];
					}else{
						imgSrc = "${ctx}/resources/images/"+suffix+".jpg";
					}
	        		$('#4-upload-more-list').append('<div class="layui-upload-img-div" id="upload_img_'+arr[2]+'"><img src="'+imgSrc+'" title="'+ arr[1] +'" class="layui-upload-img"><div class="mask"><a href="javascript:void(0)" onclick="showImg(\''+"${fileHttpPre}"+arr[3] +'\');">查看</a><a href="javascript:void(0)" onclick="del(\''+arr[2]+'\')";>删除</a></div><p>'+arr[1]+'</p><input type="hidden" value="'+deviceCotAtt+'" id="fileAtt_'+arr[2]+'" name="fileAtt4" ></input></div> ');
				}
		  	  }
	      }
	      ,allDone: function(obj){ //当文件全部被提交后，才触发
	    	  layer.closeAll('loading'); //关闭loading
	      }
	      ,error: function(){
	    	  layer.closeAll('loading'); //关闭loading
	          //上传失败
	          layer.msg("上传失败，请稍后再试~",{icon: 7});
	        }
	    });
	
   	//监听提交
  	form.on('submit(addInfo_form_submit)', function(data){
  		
  		$("#addInfo_form_submit").removeAttr("lay-submit").removeClass("layui-bg-blue").addClass("layui-btn-disabled");
  		layer.load();
       	//提交 Ajax 成功后，静态更新表格中的数据
       	$.ajax({
           	type : 'post',
			url : basePath + '/deviceCot/save',
           	data: $('#addInfo_form').serialize(),
			success:function(result){
				if(result.success){
					layer.closeAll('loading'); //关闭loading
					layer.msg('提交单据成功~',{icon: 1});
					//两秒后关闭当前页面
					setTimeout(function(){
						/**
						 * 查找父菜单，并刷新
						 */
						var topDoc = $(window.top.document);
						var path = basePath + '/deviceCot/list';
						// 获取页面名称
						var name = topDoc.find(".layui-nav-item [lay-href^='" + path + "']").text();
						if(name == '' || name == null){
							name = '设备合同';
						}
						window.parent.layui.index.openTabsPage(path, name);
						var iframe = window.parent.layui.admin.tabsBody(window.parent.layui.admin.tabsPage.index).find(".layadmin-iframe");
						iframe[0].contentWindow.location.reload(true);
						
						/**
						 * 关闭当前页
						 */
						var currPath = basePath + '/deviceCot/add';
						var layid = topDoc.find("li[lay-id^='" + currPath + "']").attr('lay-id');
						window.parent.layui.element.tabDelete('layadmin-layout-tabs', layid);//关闭当前页
				        }, 2000);
				}else{
					layer.closeAll('loading'); //关闭loading
  					$("#addInfo_form_submit").removeClass("layui-btn-disabled").addClass("layui-bg-blue").attr("lay-submit","");
					layer.msg(result.msg,{icon: 7});
			  	}
			}
       	});
   	});  
 	// data-type事件
    $('.layui-btn.layuiadmin-btn-admin').on('click', function(){
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });
 	
  	//新增-项目名select监听
    form.on('select(add_select_proId)', function(data){
    	$('#query-input-hrOrgAreId').html('');
    	$('#query-input-hrOrgAreaName').html('');
    	$('#query-input-hrOrgProId').html('');
    	$('#query-input-hrOrgProName').html('');
    	$('#query-input-carryOrg').html('');
    	$('#query-input-proLeader').html('');
    	$('#query-input-proCityName').html('');
    	var proId = data.value;
    	if(proId==''||proId==null){
    		layer.msg('请选择项目名称',{icon: 1});
    		return;
    	}else{
    		getInnerAgrtById(proId)
    	}		
	});
  	
  });
  
   function getInnerAgrtById(id){
	   var $ = layui.$
	  	,form = layui.form
		$.ajax({
			type : "POST", //提交方式  
			url : "${ctx}/deviceCot/getInnerAgrtById",//路径  
			data : {
				"id" :id
			},//数据，这里使用的是Json格式进行传输  
			success : function(result) {//返回数据根据结果进行相应的处理 
				if(result.flag==true){
					$('#proName').val(result.innerAgrtVo.proName);
					$('#types').val(result.innerAgrtVo.types);
					$('#sgCityId').val(result.innerAgrtVo.sgCityId);
					$('#proCode').val(result.innerAgrtVo.proCode);
					$('#query-input-hrOrgAreaId').val(result.innerAgrtVo.hrOrgAreaId);
					$('#query-input-hrOrgProId').val(result.innerAgrtVo.hrOrgProId);
					$('#query-input-carryOrg').val(result.innerAgrtVo.carryOrg);
					$('#query-input-proLeader').val(result.innerAgrtVo.proLeader);
					$('#query-input-hrOrgAreName').val(result.innerAgrtVo.orgAreaName);
					$('#query-input-hrOrgProName').val(result.innerAgrtVo.orgProName);
					$('#query-input-proCityName').val(result.innerAgrtVo.sgCityName);
					form.render();
				}else{
					layer.msg('数据通信异常~',{icon: 0});
				}
			} 
		});
   }
   
 	//新增一行明细
	function addRow(t){
		var $ = layui.$;
		var delBtnCount = $("button[id^='delLinkBtn']").length;
		if(delBtnCount == 1){
			$("button[id^='delLinkBtn']").each(function(){
				$(this).removeAttr("style");
			});
		}
		var thisid = $(t).attr("id").substring(10);
		thisid = $("tr[id=linkRow" + thisid + "]").find("td:first").find("input").val();
		thisid = parseInt(thisid);
		var nextIndex = parseInt(thisid) + 1;
		var maxInd = $("tr[id^='linkRow']").length;
		$("tr[id^='linkRow']").each(function(){
			var tId = $(this).attr("id").substring(7);
			tId = parseInt(tId);
		  	if(tId > maxInd){
		  		maxInd = parseInt(tId);
		  	}
		  	var thisIndex = $(this).find("td:first").find("input").val();
		  	thisIndex = parseInt(thisIndex);
		  	var nextId = thisIndex + 1;
		  	if(thisIndex > thisid) {
		  		$("tr[id=linkRow" + tId + "]").find("td:first").find("input").val(nextId);
		  		$(this).find("input").each(function(){
		  			var inputName = $(this).attr("name").split(".");
		  			var inpName = "deviceCotPrices["+thisIndex+"].";
		  			$(this).attr("name",inpName+inputName[1]);
		  		});
		  	}
		});
		var ind = maxInd + 1;
		var medalHtml = '';
		medalHtml += '<tr id="linkRow'+ind+'">';
		medalHtml += '	<td><input type="text" name="deviceCotPrices['+thisid+'].showIdx" value="'+nextIndex+'" style="border: 0px;width:50px" readonly="readonly" class="layui-input"></td>';
		medalHtml += '	<td><input type="text" name="deviceCotPrices['+thisid+'].subName" maxlength="50" onblur="checkName(this);" lay-verify="required" autocomplete="off" class="layui-input"></td>';
		medalHtml += '	<td><input type="text" name="deviceCotPrices['+thisid+'].subUnit" maxlength="50" lay-verify="required" autocomplete="off" class="layui-input"></td>';
		medalHtml += '	<td><input type="text" name="deviceCotPrices['+thisid+'].cotNums" maxlength="10" onblur="calcSumAmount(this);" lay-verify="required|floatNumber" autocomplete="off" class="layui-input"></td>';
		medalHtml += '	<td><input type="text" name="deviceCotPrices['+thisid+'].cotUnitPrice" maxlength="13" onblur="calcSumAmount(this);arabiaToCN(this,1);" lay-verify="required|floatAmount"  autocomplete="off" class="layui-input"></td>';
		medalHtml += '	<td><input type="text" name="deviceCotPrices['+thisid+'].cotSumPrice" style="border: 0px;" readonly="readonly" class="layui-input"></td>';
		medalHtml += '	<td>';
		medalHtml += '		<button type="button" class="layui-btn layui-btn-sm" id="addLinkBtn'+ind+'" onclick="addRow(this);"><i class="layui-icon">&#xe654;</i></button>';
		medalHtml += '		<button type="button" class="layui-btn layui-btn-sm" id="delLinkBtn'+ind+'" onclick="deleteRow('+ind+')"><i class="layui-icon">&#xe640;</i></button>';
		medalHtml += '	</td>';
		medalHtml += '</tr>';
		$(t).parent().parent().after(medalHtml);
		layui.form.render();
	}
	
	//删除一行
	function deleteRow(id){
		var $ = layui.$;
		layui.layer.confirm('确定删除？',{icon:3, title :'提示'}, function(index){
		   	var thisid = $("tr[id=linkRow" + id + "]").find("td:first").find("input").val();
	    	thisid = parseInt(thisid);
	    	$("tr[id^='linkRow']").each(function(){
				var tId = $(this).attr("id").substring(7);
				tId = parseInt(tId);
			  	var thisIndex = $("tr[id=linkRow" + tId + "]").find("td:first").find("input").val();
			  	thisIndex = parseInt(thisIndex);
			  	var nextId = thisIndex - 1;
			  	if(thisIndex > thisid) {
			  		$("tr[id=linkRow" + tId + "]").find("td:first").find("input").val(nextId);
			  		$(this).find("input").each(function(){
			  			var inputName = $(this).attr("name").split(".");
			  			var inpName = "deviceCotPrices["+(nextId-1)+"].";
			  			$(this).attr("name",inpName+inputName[1]);
			  		});
			  	}
			});
	    	$("#linkRow"+id).remove();
	    	var delBtnCount = $("button[id^='delLinkBtn']").length;
			if(delBtnCount == 1){
				$("button[id^='delLinkBtn']").each(function(){
					$(this).attr("style","display:none");
				});
			}
			layui.layer.close(index);
		});
	}
	
	//判断名称是否重复
	function checkName(data){
		var $ = layui.$;
        var values=[];
        $("input[name$=subName]").each(function(i,item){
            var value=$(this).val();
            if(value != ''){
            	values.push(value); //获取所有的名称
            }
        });
        if(isRepeat(values)){//判断是否有重复元素
        	layer.msg("名称已存在，请重新输入~",{icon: 7});
            $(data).val("");
        }
     }

	// 验证重复元素，有重复返回true；否则返回false
	function isRepeat(arr) {
   		var hash = {};
   		for(var i in arr) {
       		if(hash[arr[i]])
       		{
           		return true;
       		}
       		// 不存在该元素，则赋值为true，可以赋任意值，相应的修改if判断条件即可
       		hash[arr[i]] = true;
    	}
   		return false;
	}
	
	//计算合同总价
	function calcSumAmount(obj){
		var $ = layui.$;
		if (obj.value == '' || obj.value == null) {
			return;
		}
		var rgx = /^([-]?)([0-9](\d+)?(\.\d{1,2})?$)|(^\d\.\d{1,2}$)/;
		var boolrgx = rgx.test(obj.value);// true
		if(!boolrgx){
			obj.value=0;
			layer.msg('请输入正确数字，且保留两位小数！');
			var name = $(obj).attr("name");
  			var sumPrice = 0;
  			$("input[name='"+name.split('.')[0]+".cotSumPrice']").val(sumPrice.toFixed(2));
			var totalAmount = 0;
			$("input[name$=cotSumPrice]").each(function(){
  				var tId = $(this).val();
  				if(tId != '' && parseFloat(tId) > 0){
  					totalAmount = totalAmount + parseFloat(tId);
  				}
  			});
			$("#sumTotal").val(totalAmount.toFixed(2));
  		}else{
  			var name = $(obj).attr("name");
  			var sumPrice = 0;
  			if(name.split('.')[1] == 'cotNums'){
  				var price = $("input[name='"+name.split('.')[0]+".cotUnitPrice']").val();
  				if(price == ''){
  					return;
  				}else{
  					sumPrice = parseFloat(obj.value)*parseFloat(price);
  				}
  			}else if(name.split('.')[1] == 'cotUnitPrice'){
  				var nums = $("input[name='"+name.split('.')[0]+".cotNums']").val();
  				if(nums == ''){
  					return;
  				}else{
  					sumPrice = parseFloat(obj.value)*parseFloat(nums);
  				}
  			}
  			$("input[name='"+name.split('.')[0]+".cotSumPrice']").val(sumPrice.toFixed(2));
  			var totalAmount = 0;
  	  		$("input[name$=cotSumPrice]").each(function(){
  				var tId = $(this).val();
  				if(tId != '' && parseFloat(tId) > 0){
  					totalAmount = totalAmount + parseFloat(tId);
  				}
  			});
  	  		$("#sumTotal").val(totalAmount.toFixed(2));
  		}	
	}
	
	//计算合同工期（天）
	function calcDays(obj){
		var $ = layui.$;
		if (obj.value == '' || obj.value == null) {
			return;
		}
		var rgx = /^([-]?)([0-9](\d+)?(\.\d{1,2})?$)|(^\d\.\d{1,2}$)/;
		var boolrgx = rgx.test(obj.value);// true 
		if(!boolrgx){
			obj.value = 0;
			layer.msg('请输入正确数字，且保留两位小数！');
  			return;
  		}else{
			var periodDays = parseFloat(obj.value)*365;
			//判断合同工期是否有小数点，有则需加一天
			if(parseFloat(periodDays) > parseFloat((periodDays+"").split(".")[0])){
				periodDays = parseFloat((periodDays+"").split(".")[0]) + 1;
			}
	  		$("#periodDays").val(periodDays);
	  		calcDate();
		}
	}
	
	//计算竣工日期
	function calcDate(){
		var $ = layui.$;
		var periodDays = $("#periodDays").val();
		if(periodDays == ''){
			return;
		}
		var beginBuildDate = $("#beginBuildDate").val();
		if(beginBuildDate != ''){
			//合同竣工日期
			var endBuildDate = getNewData(beginBuildDate,periodDays);
  			$("#endBuildDate").val(endBuildDate);
		}
		var actBeginDate = $("#actBeginDate").val();
		if(actBeginDate != ''){
			//调整后竣工日期
			var adjustEndDate = getNewData(actBeginDate,periodDays);
  			$("#adjustEndDate").val(adjustEndDate);
		}
	}
	
	//日期加天数后得到新的日期
	function getNewData(dateTemp, days) {
	    var nDate = new Date(dateTemp);
	    var millSeconds = Math.abs(nDate) + (days * 24 * 60 * 60 * 1000);
	    var rDate = new Date(millSeconds);
	    var year = rDate.getFullYear();
	    var month = rDate.getMonth() + 1;
	    month = month < 10 ? "0"+month : month;
	    var date = rDate.getDate();
	    date = date < 10 ? "0"+date : date;
	    return (year + "-" + month + "-" + date);
	}
	// 请输入正确数字，且保留两位小数！
	function checkValueNumber(obj) {
		var $ = layui.$, form = layui.form
		if (obj.value == '' || obj.value == null) {
			return;
		}
		var rgx = /^([-]?)([0-9](\d+)?(\.\d{1,2})?$)|(^\d\.\d{1,2}$)/;
		var boolrgx = rgx.test(obj.value);// true 
		if (!boolrgx){
			obj.value=0;
			layer.msg('请输入正确数字，且保留两位小数！');
		}
	}
	//删除文件
	function del(index){
		layui.$("#fileAtt_"+index).remove();
		layui.$("#upload_img_"+index).remove();
	}
  
	//根据url查看文件
	function showImg(url){
		var openWin = window.open(url,'','left=410,height=768, width=1024, top=200,  toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no');
	}
  </script>
</body>
</html>

